/*
 * SerialObjectsDao.java
 *
 * Created on 9 de octubre de 2006, 05:08 PM
 *
 
 */

package inc.jmc.cyberboard.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.log4j.Logger;

/**
 *
 * @author Juan Miguel Olguin Salguero
 */
public class SerialObjectsDao {
    
    static Logger logger = Logger.getLogger(SerialObjectsDao.class.getName());
    
    /** Creates a new instance of SerialObjectsDao */
    public SerialObjectsDao() {
    }
    public static void unInvited()
    {
    	try {
			java.sql.Connection conn=Dao.getConnection();
			String update="update ccn_uninvited set uninvited=1";
			PreparedStatement pstmt=conn.prepareStatement(update);
	        int updated=pstmt.executeUpdate();
	        logger.info("Los registros actualizados son: "+updated);
	        pstmt.close();
	        conn.close();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
    	
    	
    }
    public static boolean isUninvited()
    {
    	int value=0;
        try {
			java.sql.Connection conn=Dao.getConnection();
			String update="SELECT * FROM ccn_uninvited";
			PreparedStatement pstmt=conn.prepareStatement(update);
	        ResultSet rs=pstmt.executeQuery();
	        while(rs.next())
	        {
	        	value=rs.getInt("uninvited");
	        }
	        rs.close();
	        pstmt.close();
	        conn.close();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return value==1?true:false;
    	
    	
    }
    public int update(Connection conn,String className,String xml) throws java.sql.SQLException
    {
        String update ="update CCN_SERIAL_OBJECTS set SERIAL_XML=?, fecha_update=now()" +
                "where class_name=?";
        PreparedStatement pstmt=conn.prepareStatement(update);
        pstmt.setString(1,xml);
        pstmt.setString(2,className);
        int updated=pstmt.executeUpdate();
        logger.info("Los registros actualizados son: "+updated);
        pstmt.close();
        return 0;
    }
    public String query(Connection conn,String className) throws java.sql.SQLException
    {
        String update ="select SERIAL_XML from CCN_SERIAL_OBJECTS " +
                "where class_name=?";
        PreparedStatement pstmt=conn.prepareStatement(update);
        pstmt.setString(1,className);
        ResultSet rs=pstmt.executeQuery();
        String serial=null;
        while(rs.next())
        {
            serial=rs.getString("SERIAL_XML");
        }
        rs.close();
        pstmt.close();
        return serial;
    }
    
}
